package com.dinadi.yidi.ui.fragment;

import android.content.Intent;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.dinadi.yidi.R;
import com.dinadi.yidi.adapter.DesignerWorksAdapter;
import com.dinadi.yidi.base.BaseFragment;
import com.dinadi.yidi.bean.DesignerWorksBean;
import com.dinadi.yidi.bean.DesignerWorksRequst;
import com.dinadi.yidi.bean.DesignerWorksRequstEntity;
import com.dinadi.yidi.http.HttpListener;
import com.dinadi.yidi.http.interfaces.CommonInterface;
import com.dinadi.yidi.ui.activity.ProductDetailsActivity;
import com.dinadi.yidi.utils.JsonUtil;
import com.dinadi.yidi.utils.SharePreferencesHelper;
import com.dinadi.yidi.utils.pullloadmorerecyclerview.PullLoadMoreRecyclerView;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * author: JiaYu.He
 * Date: 2017/4/5    设计师作品
 * Time: 17:24
 */

public class DesignerWorksFragment extends BaseFragment {
    private View view;
    private PullLoadMoreRecyclerView mPullLoadMoreRecyclerView;
    private DesignerWorksAdapter mRefreshAdapter;
    private List<DesignerWorksRequstEntity> dataList = new ArrayList<>();
    private int page = 1;
    private String userId;

    @Override
    protected void noWifiRefresh() {

    }

    @Override
    protected View getLayout() {
        view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_designer_works, null);
        return view;
    }

    @Override
    protected void initView() {
        changePager(2);
        mPullLoadMoreRecyclerView = (PullLoadMoreRecyclerView) view.findViewById(R.id.pullLoadMoreRecyclerView);
        mPullLoadMoreRecyclerView.setGridLayout(2);
        mRefreshAdapter = new DesignerWorksAdapter(getActivity());
        mPullLoadMoreRecyclerView.setAdapter(mRefreshAdapter);
        mPullLoadMoreRecyclerView.setOnPullLoadMoreListener(new PullLoadMoreListener());
        mPullLoadMoreRecyclerView.setPullRefreshEnable(false);
    }


    @Override
    protected void initListener() {

        mRefreshAdapter.setmOnItemClickListener(new DesignerWorksAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, String data) {
                Intent intent = new Intent(getActivity(), ProductDetailsActivity.class);
                intent.putExtra("productId", data);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    protected void clickEvent(View v) {

    }

    @Override
    protected void initData() {
        userId = SharePreferencesHelper.getInstance(getActivity()).getString("dUserId");
        initPullrefresh();

    }

    HttpListener httpListener = new HttpListener() {

        @Override
        public void OnSuccess(String response) {
            super.OnSuccess(response);
            String errCode = JsonUtil.getFieldValue(response, "errCode");
            String errMsg = JsonUtil.getFieldValue(response, "errMsg");
            if (errCode.equals("0000")) {
                //  closeLoadingDialog();
                DesignerWorksRequst bean = JsonUtil.parseJsonToBean(response, DesignerWorksRequst.class);
                dataList = bean.getProductList();
                mRefreshAdapter.addAllData(dataList);
                getData();
            } else {
                toast(errMsg);
            }

        }

        @Override
        public void OnFail(String errorResponse) {
            super.OnFail(errorResponse);
            toast("加载失败");
        }
    };

    private void getData() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //刷新结束
                mPullLoadMoreRecyclerView.setPullLoadMoreCompleted();
            }
        }, 1000);
    }

    class PullLoadMoreListener implements PullLoadMoreRecyclerView.PullLoadMoreListener {
        @Override
        public void onRefresh() {
            setRefresh();
        }

        @Override
        public void onLoadMore() {
            page = page + 1;
            initPullrefresh();
        }

    }

    private void setRefresh() {
        mRefreshAdapter.clearData();
        page = 1;
        initPullrefresh();
    }

    private void initPullrefresh() {

        DesignerWorksBean bean = new DesignerWorksBean();
        //  bean.setToken(token);
        bean.setUserId(userId);
        bean.setPageNum(page + "");
        bean.setPageSize("10");
        CommonInterface designerWorksInterface = new CommonInterface(getActivity(), httpListener);
        designerWorksInterface.getInterface("13002", JSON.toJSONString(bean));
    }

}
